package com.ljp.makefriend.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljp.makefriend.pojo.Team;
import com.ljp.makefriend.pojo.User;
import com.ljp.makefriend.pojo.vo.TeamUserVo;
import com.ljp.makefriend.pojo.vo.UserVo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@SpringBootTest
public class hasJoinTeam {

    @Resource
    private UserService userService;

    @Resource
    private TeamService teamService;


    @Test
    void hasJoinTeamTest(){
        QueryWrapper<Team> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(qw -> qw.gt("expireTime", new Date()).or().isNull("expireTime"));
        List<Team> teamList = teamService.list(queryWrapper);
        // 最终返回的列表
        List<TeamUserVo> teamUserVoList = new ArrayList<>();
        // 关联查询创建人的用户信息
        for (Team team: teamList) {
            Long userId = team.getUserId();     // 获取创建人的 id
            TeamUserVo teamUserVo = new TeamUserVo();
            if (userId == null) {
                continue;
            }
            User user = userService.getById(userId);    // 根据 id 获取创建人的信息
            BeanUtils.copyProperties(team, teamUserVo); // 将 team 对象的内容给 teamUserVo
            // 用户信息脱敏
            UserVo userVo = new UserVo();
            if (user != null) {
                BeanUtils.copyProperties(user, userVo);     // 将 user 对象的内容给 userVo
                teamUserVo.setCreatUser(userVo);    // 封装队伍创建人的信息
            }
            teamUserVoList.add(teamUserVo);     // 最早队伍信息和创建人的信息添加进入 teamUserVoList
        }

        List<TeamUserVo> teamUserVoList1 = teamService.hasJoinTeam(teamUserVoList, 1);
        for (TeamUserVo item: teamUserVoList1) {
            System.out.println(item);
        }
    }
}
